﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using TeamOPSchoolCup.Domain.POCO;
using TeamOPSchoolCup.Domain.DAL;
using TeamOPSchoolCup.Domain.Services;

namespace TeamOPSchoolCup.Web.Controllers
{
  public class RegionController : Controller
  {
    //
    // GET: /Region/


    public ViewResult Index(int? regionId) {
      //return View(RegionService.GetRegionsByHierarchy(true));
      if (regionId == null) return View(RegionService.GetContinents(true));

      Region region = RegionService.GetRegion(regionId.Value); //.Value since it's a nullable int
      List<Region> children = RegionService.GetChildren(region);
      ViewBag.RegionId = region.RegionId;
      ViewBag.ParentId = region.ParentId;
      ViewBag.ParentName = region.Name;

      return View(children);
    }

    //
    // GET: /Region/Create

    public ActionResult Create(int? parentId) {
      ViewBag.Parent = (parentId.HasValue ? RegionService.GetRegion(parentId.Value) : null);
      return View();
    }

    //
    // POST: /Region/Create

    [HttpPost]
    public ActionResult Create(Region region) {
      if (ModelState.IsValid) {
        region.DateJoined = DateTime.Today;

        RegionService.AddRegion(region);
        return RedirectToAction("Index", new { regionId = region.ParentId });
      }
      ViewBag.ParentId = new SelectList(RegionService.GetAllRegions(), "RegionId", "Name", region.ParentId);
      return View(region);
    }

    public ActionResult addRegions() {
      return View();
    }

    [HttpPost]
    public ActionResult addRegions(HttpPostedFileBase file) {
      if (file == null || file.ContentLength <= 0 || file.ContentType != "application/vnd.ms-excel") return RedirectToAction("Index");
      using (EFDbContext db = new EFDbContext()) {
        string path = Path.Combine(Server.MapPath("~/Upload"), file.FileName);
        file.SaveAs(path);

        string line;
        using (StreamReader r = new StreamReader(path)) {
          Dictionary<int, Region> lastItems = new Dictionary<int, Region>();
          while ((line = r.ReadLine()) != null) {
            if (!ModelState.IsValid) continue;
            string[] values = line.Split(';');
            for (int i = 0; i < values.Length; i++) {
              if (String.IsNullOrWhiteSpace(values[i])) continue;
              Region region = null;
              if ((region = RegionService.GetRegion(db, values[i])) == null) {
                region = new Region() { Name = values[i], DateJoined = DateTime.Today };
                if (lastItems.ContainsKey(i - 1)) {
                  region.Parent = lastItems[i - 1];
                }
                RegionService.AddRegion(db, region);
              }
              lastItems.Remove(i);
              lastItems.Add(i, region);
            }
          }
        }
      }

      return RedirectToAction("Index");
    }

    //
    // GET: /Region/Edit/5

    public ActionResult Edit(int id) {
      Region region = RegionService.GetRegion(id);
      ViewBag.ParentId = new SelectList(RegionService.GetAllRegions(), "RegionId", "Name", region.ParentId);
      return View(region);
    }

    //
    // POST: /Test/Edit/5

    [HttpPost]
    public ActionResult Edit(Region region) {
      if (ModelState.IsValid) {
        RegionService.UpdateRegion(region);
        return RedirectToAction("Index");
      }
      ViewBag.ParentId = new SelectList(RegionService.GetAllRegions(), "RegionId", "Name", region.ParentId);
      return View(region);
    }
  }
}